package wei.xinya.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Date;

@Aspect
@Component
public class MyAspect {

    @Pointcut("execution( * *..impl.MyServiceImpl.*())")
    public void myPointcut() {
    }

    @Before(value = "myPointcut()")
    public void beforeDo1() {
        System.out.println("前置通知: 开始了 " + new Date());
    }

    @AfterReturning(value = "myPointcut()", returning = "result")
    public void afterReturn(Object result) {
        System.out.println("后置通知: " + result);
    }

    @Around(value = "myPointcut()")
    public Object myAround(ProceedingJoinPoint pjp) {
        System.out.println("环绕通知1");
        Object proceed = null;
        try {
            proceed = pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }

        System.out.println("环绕通知2");
        return proceed;
    }

    @After(value = "myPointcut()")
    public void after() {
        System.out.println("最终通知: last " + new Date());
    }

}
